package com.example.demo.service;

import com.example.demo.mapper.*;
import com.example.demo.model.*;
import com.example.demo.tools.Constant;
import com.example.demo.tools.EncryptionTool;
import com.example.demo.tools.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJ
 * Date: 2023-02-17
 * Time: 21:36
 */
@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ActiveMapper activeMapper;

    @Autowired
    private OlderMapper olderMapper;

    @Autowired
    private EmployeeCommentMapper employeeCommentMapper;

    @Autowired
    private EncryptionTool encryptionTool;      // 加密工具

    @Autowired
    private RedisUtil redisUtil;

//    获取首页头像下方的表格信息
    public Object getPersonTable(LoginUser user){
        List<LoginUser> list = adminMapper.getAllPersonInfo();
        if(list != null){
            return list;
        }
        return "获取所有人员基础信息失败!";
    }

//    查询所有的管理员信息
    public Object getAllAdminInfo(LoginUser user) {
        if(user != null && user.getId() != null){
            List<Admin> adminList = adminMapper.getAdminList(user.getId());
            if(adminList != null){
                return adminList;
            }
        }
        return "管理员信息查询失败!";
    }

//    根据 adminId 查询下属员工的所有信息
    public Object getAllEmployeeByAdminId(Integer adminId) {
        List<Employee> employeeListByAdminId = employeeMapper.getEmployeeListByAdminId(adminId);
        if(employeeListByAdminId != null){
            return employeeListByAdminId;
        }
        return "查询下属员工失败!";
    }

//    获取当前登录用户下所管理的全部员工信息
    public Object getAllEmployee(LoginUser user) {
        List<Employee> allEmployee = employeeMapper.getAllEmployee(user.getId());
        String adminName = adminMapper.getAdminByID(user.getId());
        if(allEmployee != null){
            for (Employee employee : allEmployee) {
                employee.setAdmin_name(adminName);
            }
            return allEmployee;
        }
        return "查询当前登录管理员的下属员工失败!";
    }

//    重置员工分数
    public Object updateEmployeeGrade(Integer id) {
        Integer integer = employeeMapper.updateEmployeeGrade(id);
        if(integer == 1){
            // 表示分数重置成功
            return true;
        }
        return "分数重置失败!";
    }

//    更新员工信息
    public Object updateEmployeeInfo(Integer id, String username, Integer age, String phone, String area, Integer adminId,String address,String appraise) {
        // 1.更新人员总表中的数据
        Integer n1 = employeeMapper.updateEmployeeInfo(id, username, age, phone);
        // 2.更新员工细节表中的数据
        Integer n2 = employeeMapper.updateEmployeeInfoDetial(id, area, adminId, address, appraise);
        if(n1 == 1 && n2 == 1){
            return true;
        }
        return "修改员工信息失败!";
    }
//    删除员工
    public Object deleteEmployeeForAdmin(Integer id) {
        // 1. 删除person中的员工信息
        Integer n1 = employeeMapper.deleteEmployeeForAdmin(id);
        // 2. 删除employee_detail中的员工细节数据
        Integer n2 = employeeMapper.deleteEmployeeForAdminInDetail(id);
        // 3. 删除employee_older表中的内容(如果有就删除)
        employeeCommentMapper.deleteEmployeeOlderByEmployeeId(id);
        if(n1 == 1 && n2 == 1){
            return true;
        }
        return "删除员工失败!";
    }

//    查询所有管理员信息(只有 id 和 username 字段)
    public Object getAllAdmin() {
        List<LoginUser> adminTotal = adminMapper.getAdminTotal();
        if(adminTotal != null){
            return adminTotal;
        }
        return "查询所有管理员信息失败!";
    }

//    获取当前管理员下的所有活动信息
    public Object getActiveInfoByAdmin(Integer id) {
        List<Active> activeList = activeMapper.getAllActiveProve(id);
        if(activeList != null){
//            查找员工名
            for (Active active:activeList) {
                List<LoginUser> employeeList = employeeMapper.getEmployeeList(active.getActive_employee());
                active.setActive_employee_name(employeeList.get(0).getUsername());
            }
            return activeList;
        }
        return "查询所有活动信息失败!";
    }



//    活动通过
    public Object activeBy(Integer id) {
        Integer result = activeMapper.activeBy(id);
        if(result == 1){
            return true;
        }
        return "活动审批失败!";
    }

//    活动删除
    public Object activeDelete(Integer id) {
        Integer result = activeMapper.activeDelete(id);
        if(result == 1){
            return true;
        }
        return "活动删除失败!";
    }

//    获取所有的活动信息
    public Object getAllActiveInfo() {
        List<Active> allActive = activeMapper.getAllActive();
        if(allActive != null){
            for (Active active:allActive) {
                List<LoginUser> result = employeeMapper.getEmployeeList(active.getActive_employee());
                active.setActive_employee_name(result.get(0).getUsername());
            }
            return allActive;
        }
        return "获取活动信息失败!";
    }

//    通过活动id获取所有的评论信息
    public Object getAllCommentByActiveId(Integer id) {
        List<Comment> activeList = activeMapper.getAllCommentByActiveId(id);
        if(activeList != null){
            for (Comment comment:activeList) {
                List<LoginUser> users = adminMapper.getAllCommentPersonInfo(comment.getComment_id());
                comment.setComment_name(users.get(0).getUsername());
                comment.setComment_photo(users.get(0).getPhoto_url());
                comment.setStatus(users.get(0).getStatus());
            }
            return activeList;
        }
        return "获取评论失败!";
    }

//    添加评论
    public Object addComment(Comment comment) {
        Integer result = activeMapper.addComment(comment);
        if(result == 1){
            return true;
        }
        return "添加评论失败!";
    }

//    活动的修改
    public Object updateDetailActiveInfo(Active active) {
        Integer result = activeMapper.updateActiveInfo(active);
        if(result == 1){
            return true;
        }
        return "修改活动信息失败!";
    }

//    修改管理员信息
    public Object updateAdminInfo(Integer id, String username, String password, String sex, Integer age, String phone, String url,String address) {
        Integer result = -1;
        if(password == null || "".equals(password)){
            result = adminMapper.updateAdminInfo(id, username, null, sex, age, phone, url);
        }else{
            // 给密码加密
            String encrypt = encryptionTool.encrypt(password);
            result = adminMapper.updateAdminInfo(id, username, encrypt, sex, age, phone, url);
        }

        if(result == 1){
            // 更新 admin_detail 中的信息
            Integer n = adminMapper.updateAadminInfoInDetail(id, address);
            if(n == 1){
                // 1.移除 redis 中的信息
                redisUtil.deleteObject(Constant.USER_LOGIN+id);
                return true;
            }
        }
        return "修改管理员信息失败!";
    }

//    根据条件查询（管理员模块的查询）
    public Object getAllAdminInfoByUsernameOrAgeD(Integer adminId,String username, String phone, String sex, Integer age) {
        List<Admin> list = adminMapper.getAllAdminByUsernameOrAgeD(adminId,username, phone, sex, age);
        if(list != null){
            return list;
        }
        return "根据条件查询管理员失败!";
    }

//    获取老人用户给员工评价的信息
    public Object getEmployeeOlderComment() {
        List<EmployeeOlder> list = employeeCommentMapper.getEmployeeOlderComment();
        if(list != null){
            for (EmployeeOlder employeeOlder:list) {
                // 查询员工的name、phone、address
                Employee employee = employeeMapper.getDetailInfoByEmployeeId(employeeOlder.getEmployee_id());
                if(employee == null){
                    return "查询员工信息失败!";
                }
                employeeOlder.setEmployee_name(employee.getUsername());
                employeeOlder.setEmployee_phone(employee.getPhone());
                employeeOlder.setEmployee_address(employee.getAddress());

                // 查询老人的name、phone、address
                Older older = olderMapper.getOlderAllInfo(employeeOlder.getOlder_id());
                if(older == null){
                    return "查询老人信息失败!";
                }
                employeeOlder.setOlder_name(older.getUsername());
                employeeOlder.setOlder_phone(older.getPhone());
                employeeOlder.setOlder_address(older.getAddress());
            }
            return list;
        }
        return "查询老人评价员工信息失败!";
    }
}
